package com.fanyouzhao.ssm251.service.impl;

import com.fanyouzhao.ssm251.service.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

/**
 * 通用Service的实现类
 * 1.在接口和实现类上都需要<T>
 * 2.不希望该类被实例化
 * 3.不需要@Service
 * 4.需要实现所有的BaseService方法
 * @author: fanyouzhao
 * @date: 14:27 2021/6/22
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {
    //如何选择自动装配的注解?
    //当子类继承的时候,需要: Mapper<User> Mapper<Admin>...
    // Spring在4之后提供一种注入机制:泛型依赖注入
    @Autowired
    Mapper<T> mapper;

    @Override
    public T selectById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public List<T> select(T t) {
        return mapper.select(t);
    }

    @Override
    public List<T> selectByExample(T t) throws IllegalAccessException {
        Example example = new Example(t.getClass());
        Example.Criteria criteria = example.createCriteria();
//        通过t反射得到字段列表,遍历获取非空字段
        Class<?> clazz = t.getClass();
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            Object value = declaredField.get(t);
            String name = declaredField.getName();
            if(value!=null){
                criteria.andLike(name,"%"+value.toString()+"%");
            }
        }
        List<T> list = mapper.selectByExample(example);
        return list;
    }

    @Override
    public PageInfo<T> selectByPage(Integer page, Integer size, T... t) {
        PageHelper.startPage(page,size);

        List<T> ts = null;
        if (t.length>0){
            ts = mapper.select(t[0]);
        }else {
            ts = mapper.selectAll();
        }
        PageInfo<T> pageInfo = PageInfo.of(ts);

        return pageInfo;
    }

    @Override
    public void add(T t) {
        int i = mapper.insertSelective(t);
    }

    @Override
    public void updateById(T t) {
        int i = mapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void deleteById(Integer id) {
        int i = mapper.deleteByPrimaryKey(id);
    }
}
